﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Xml;

namespace Georeferencer
{
   public class GeoRefPoint
   {
      public GeoRefPoint(UInt32 x, UInt32 y)
      {
         m_x = x;
         m_y = y;

      }
      public UInt32 m_x = 0;
      public UInt32 m_y = 0;
      public Double m_lat = 0.0;
      public Double m_lon = 0.0;
   }

   public class GeoRefSection
   {
      List<GeoRefPoint> m_geoRefPoints = new List<GeoRefPoint>();
   }

   public class GeoRef
   {
      List<GeoRefSection> m_geoRefSections = new List<GeoRefSection>();
   }

   public partial class MainPage : System.Web.UI.Page
   {
      static UInt32 m_numberOfAreas = 0;

      static GeoRef m_geoRef = new GeoRef();

      protected void Page_Load(object sender, EventArgs e)
      {
         

         m_gmap.Key = "ABQIAAAAHSU9wFYOjdZ5aRcK4MQjoxTeAque60tttfvPTB_lkWPtPeSQMBQegYRj0bfkpwv1pOaALkGqALZDsg";
         m_gmap.serverEventsType = Subgurim.Controles.GMap.ServerEventsTypeEnum.AspNetPostBack;
         m_gmap.enableServerEvents = true;
      
         //Response.ContentType = "image/png";
         //Bmp.Save(Response.OutputStream, ImageFormat.Gif);
         //Response.Write("<img src=\"generated_image.jpg\" />");

         drawDrawnMap();
         m_drawnMap.ImageUrl = "generated_image.jpg";
      }

      protected void m_addNewAreaButton_Click(object sender, EventArgs e)
      {
         m_numberOfAreas++;
         m_point1LatTextBox.Enabled = true;
         m_point1LonTextBox.Enabled = true;
         m_point1XTextBox.Enabled = true;
         m_point1YTextBox.Enabled = true;
      }

      protected string m_gmap_Click(object s, Subgurim.Controles.GAjaxServerEventArgs e)
      {
         //Bmp.SetPixel(30, 30, Color.Yellow);
         //Bmp.Save(Server.MapPath("generated_image.jpg"), ImageFormat.Jpeg);
         m_gmap.addGMarker(new Subgurim.Controles.GMarker(e.point));
        

         return default(string);
      }

      protected void m_drawnMap_Click1(object sender, ImageClickEventArgs e)
      {
         //m_geoRefPoints.Add(new GeoRefPoint((UInt32)e.X, (UInt32)e.Y));

         drawDrawnMap();
      }

      protected void drawDrawnMap()
      {
         Graphics g;
         string path = Server.MapPath("~");

         Bitmap drawnMapBitmap = new Bitmap(path + "Zell_am_See.jpg");

         g = Graphics.FromImage(drawnMapBitmap);

         //foreach (GeoRefPoint geoPoint in m_geoRefPoints)
         //{
         //   g.DrawLine(Pens.Aqua, 0, 0, geoPoint.m_x, geoPoint.m_y);
         //}

         //Bmp.SetPixel(e.X, e.Y, Color.Yellow);
         drawnMapBitmap.Save(Server.MapPath("generated_image.jpg"), ImageFormat.Jpeg);

         //m_point1XTextBox.Text = m_x.ToString();
      }

      protected void m_resetButton_Click(object sender, EventArgs e)
      {
        // m_geoRefPoints.Clear();
         drawDrawnMap();
      }

      protected void m_saveButton_Click(object sender, EventArgs e)
      {
         FileStream fs = new FileStream("bob.xml", FileMode.Create);
         XmlTextWriter w = new XmlTextWriter(fs, null);

         w.WriteStartDocument();
         w.WriteStartElement("flap");
         w.WriteComment("clappa flappa");
         w.WriteStartElement("boo");
         w.WriteAttributeString("ID", "1");
         w.WriteStartElement("goo");
         w.WriteString("foo");
         w.WriteEndElement();
         w.WriteEndElement();
         w.WriteEndElement();
         w.WriteEndDocument();
         w.Close();

      }
   }
}